package com.dataart.training.crimequalification.service;


import com.dataart.training.crimequalification.dao.ApplicationEnvironmentDAO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.TimeZone;

@Service     // TODO: comment
@Transactional
public class ApplicationEnvironmentServiceImpl implements ApplicationEnvironmentService {

    //--------------------------------------------------------------------------------

    @Autowired
    private ApplicationEnvironmentDAO environmentDAO;

    //--------------------------------------------------------------------------------

    private static final String SCHEME_VAR = "scheme";
    private static final String HOST_NAME_VAR = "hostname";
    private static final String PORT_VAR = "port";
    private static final String SMS_HANDLER_URL_PATTERN_VAR = "sms_handler_url_pattern";
    private static final String DEFAULT_TIMEZONE_VAR = "default_app_timezone";
    private static final String TWILIO_ACCOUNT_SID = "twilio_account_sid";
    private static final String TWILIO_AUTHORIZATION_TOKEN = "twilio_auth_token";
    private static final String TWILIO_ACCOUNT_NUMBER = "twilio_account_number";

    //--------------------------------------------------------------------------------

    /**
     * @param variableName
     * @return
     */
    private String getApplicationEnvironmentVariable(String variableName) {

        String variableValue = environmentDAO.getApplicationEnvironmentVariableByName(variableName);
        if (variableValue == null) {
            throw new IllegalStateException("No application environment variable under name \"" +
                    variableName + "\" present.");
        }

        return variableValue;
    }

    /**
     * @return
     */
    @Override
    public String getHostName() {
        return getApplicationEnvironmentVariable(HOST_NAME_VAR);
    }

    /**
     * @return
     */
    @Override
    public String getScheme() {
        return getApplicationEnvironmentVariable(SCHEME_VAR);
    }

    /**
     * @return
     */
    @Override
    public String getPort() {
        return getApplicationEnvironmentVariable(PORT_VAR);
    }

    /**
     * @return
     */
    @Override
    public String getSmsHandlerURLPattern() {
        return getApplicationEnvironmentVariable(SMS_HANDLER_URL_PATTERN_VAR);
    }

    /**
     * Returns default timezone, fallback enabled
     *
     * @return
     */
    @Override
    public TimeZone getDefaultTimezone() {
        return TimeZone.getTimeZone(getApplicationEnvironmentVariable(DEFAULT_TIMEZONE_VAR));
    }

    /**
     * @return
     */
    @Override
    public String getTwilioAccountSid() {
        return getApplicationEnvironmentVariable(TWILIO_ACCOUNT_SID);
    }

    /**
     * @return
     */
    @Override
    public String getTwilioAuthorizationToken() {
        return getApplicationEnvironmentVariable(TWILIO_AUTHORIZATION_TOKEN);
    }

    /**
     *
     * @return
     */
    @Override
    public String getTwilioAccountNumber() {
        return getApplicationEnvironmentVariable(TWILIO_ACCOUNT_NUMBER);
    }

}
